package com.zero.cloud.filter;


import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/*
*   全局过滤器
* */
@Component   // 放入spring容器
public class GlobalFilter implements org.springframework.cloud.gateway.filter.GlobalFilter {


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        // 获取请求对象 request
        ServerHttpRequest request = exchange.getRequest();
        // 获取响应对象 response
        ServerHttpResponse response = exchange.getResponse();

        /*
        *   对请求进行检验、处理...，如果不满足条件，直接响应，不放行
        * */
        System.out.println("全局前置过滤器");

        // 放行
        return chain.filter(exchange).then(Mono.fromRunnable(()->{
            /*
            *   对响应结果进行处理
            * */
            System.out.println("全局后置过滤器");
        }));
    }


}
